<#assign hasDateTimeFormat = false>
<#assign hasDateFormat = false>
<#assign hasTimeFormat = false>
<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${r'${drawerProps.title}'}-${tableComment!}`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <!-- 表单内容-->
      <#list fieldInfoList as field>
      <#if (field.showInSave || field.showInUpdate) && field.formType??>
      <#-- 这里v-if只有该字段只更新或者只在新增时出现的情况,为什么这里不用v-show,如果某个字段设置了规则必填,但是又隐藏了就无法提交 -->
      <el-form-item <#if !(field.showInSave && field.showInUpdate) >v-if="drawerProps.<#if field.showInSave>isSave<#elseif field.showInUpdate>isUpdate</#if>" </#if>label="${field.comment}" prop="${field.fieldName}">
        <#if field.formType.type="el-input">
        <el-input v-model="operateDataInfo.${field.fieldName}" placeholder="请输入${field.comment}" clearable></el-input>
        <#elseif field.formType.type="el-input-number">
        <el-input-number v-model="operateDataInfo.${field.fieldName}" clearable />
        <#elseif field.formType.type="el-select">
        <el-select v-model="operateDataInfo.${field.fieldName}" filterable placeholder="请选择${field.comment}">
          <el-option
            v-for="item in drawerProps.enumMap?.get('${field.fieldName}')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <#elseif field.formType.type="textarea">
        <el-input
          type="textarea"
          v-model="operateDataInfo.${field.fieldName}"
          placeholder="请输入${field.comment}"
          clearable
        ></el-input>
        <#elseif field.formType.type="el-switch">
        <el-switch v-model="operateDataInfo.${field.fieldName}" inline-prompt />
        <#elseif field.formType.type="date-picker">
        <#assign hasDateFormat = true>
        <el-date-picker v-model="operateDataInfo.${field.fieldName}" type="date" placeholder="请选择${field.comment}" :value-format="DATE_FORMAT" />
        <#elseif field.formType.type="datetime-picker">
        <#assign hasDateTimeFormat = true>
        <el-date-picker
          v-model="operateDataInfo.${field.fieldName}"
          type="datetime"
          placeholder="请选择${field.comment}"
          :value-format="DATE_TIME_FORMAT"
        />
        <#elseif field.formType.type="time-picker">
        <#assign hasTimeFormat = true>
        <el-time-picker v-model="operateDataInfo.${field.fieldName}" placeholder="请选择${field.comment}" :value-format="TIME_FORMAT" />
        <#elseif field.formType.type="radio-group">
        <el-radio-group v-model="operateDataInfo.${field.fieldName}">
          <el-radio
            v-for="item in drawerProps.enumMap?.get('${field.fieldName}')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-radio-group>
        <#elseif field.formType.type="radio-button">
        <el-radio-group v-model="operateDataInfo.${field.fieldName}">
          <el-radio-button
            v-for="item in drawerProps.enumMap?.get('${field.fieldName}')"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-radio-group>
        </#if>
      </el-form-item>
      </#if>
      </#list>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
<#assign api_result = "">
<#if genPage>
  <#if api_result?has_content>
  <#assign api_result = api_result + ", " + pageResult.className>
  <#else>
  <#assign api_result = pageResult.className>
  </#if>
</#if>
<#if genSave>
  <#if api_result?has_content>
  <#assign api_result = api_result + ", " + saveParam.className>
  <#else>
  <#assign api_result = saveParam.className>
  </#if>
</#if>
<#if genUpdate>
  <#if api_result?has_content>
  <#assign api_result = api_result + ", " + updateParam.className + ", get" + entity.className + "Info">
  <#else>
  <#assign api_result = updateParam.className + ", get" + entity.className + "Info">
  </#if>
</#if>
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
<#if api_result?has_content>
import { ${api_result} } from "@/api/modules/${moduleName}/${entity.className}";
</#if>
<#if hasDateTimeFormat || hasDateFormat || hasTimeFormat>
import { <#if hasDateTimeFormat>DATE_TIME_FORMAT<#--
        --><#elseif hasDateFormat>DATE_FORMAT<#--
        --><#elseif hasTimeFormat>TIME_FORMAT</#if> } from "@/utils";
</#if>

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<<#if genPage>${pageResult.className}<#else>{}</#if>>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  // 表单规则, 例如: userName: [{ required: true, message: "请输入用户名" }]
  <#assign result = "">
  <#list fieldInfoList as field>
    <#if (field.showInSave || field.showInUpdate) && field.formType?? && field.required>
      <#if result?has_content>
        <#assign result = result + ", ">
      </#if>
      <#assign result = result + "${field.fieldName}: [{ required: true, message: \"请输入${field.comment}\" }]">
    </#if>
  </#list>
  ${result}
});

const operateDataInfo = ref<<#if genSave>${saveParam.className}</#if><#if genSave && genUpdate> | </#if><#if genUpdate>${updateParam.className}</#if>>({});
const openDrawer = async (params: DrawerProps) => {
  <#if genUpdate>
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await get${entity.className}Info(params.row.id ? params.row.id : "");
    operateDataInfo.value = { ...data };
  }
  </#if>

  <#if genSave>
  if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = {};
  }
  </#if>
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${r"${drawerProps.value.title}"} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
